package com.itheima.wemedia.controller;

import com.itheima.admin.pojo.AdChannel;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.AuthReqDto;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.pojo.WmUser;
import com.itheima.wemedia.service.ApiService;
import com.itheima.wemedia.service.WmMaterialService;
import com.itheima.wemedia.service.WmNewsService;
import com.itheima.wemedia.service.WmUserService;
import com.itheima.wemedia.vo.WmNewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0
 * @description 所有提供远程调用
 * @package com.itheima.wemedia.controller
 */
@RestController
public class ApiController {

    @Resource
    private WmUserService wmUserService;

    @Resource
    private WmMaterialService wmMaterialService;

    @Resource
    private ApiService apiService;

    @Resource
    private WmNewsService wmNewsService;

    /**
     * 通过apUserId查询自媒体账号信息
     * @param apUserId
     * @return
     */
    @GetMapping("/api/wmUser/getByApUserId/{apUserId}")
    public ResultVo<WmUser> getByApUserId(@PathVariable(value = "apUserId") Long apUserId){
        WmUser wmUser = wmUserService.getByApUserId(apUserId);
        return ResultVo.ok(wmUser);
    }

    /**
     * 添加自媒体账号
     * @param wmUser
     * @return
     */
    @PostMapping("/api/wmUser/add")
    public ResultVo<WmUser> addWmUser(@RequestBody WmUser wmUser){
        wmUserService.save(wmUser);
        return ResultVo.ok(wmUser);
    }

    /**
     * 添加素材 远程调用接口
     * @param wmMaterial
     * @return
     */
    @PostMapping("/api/wmMaterial/add")
    public ResultVo addWmMaterial(@RequestBody WmMaterial wmMaterial){
        wmMaterialService.save(wmMaterial);
        return ResultVo.ok("操作成功!");
    }

    /**
     * 频道列表
     * @return
     */
    @GetMapping("/channel/channels")
    public ResultVo<List<AdChannel>> list(){
        List<AdChannel> list = apiService.channels();
        return ResultVo.ok(list);
    }

    /**
     * 通过id查询文章信息
     * @param wmNewsId
     * @return
     */
    @GetMapping("/api/wmNews/{wmNewsId}")
    public ResultVo<WmNews> getWmNewsById(@PathVariable(value = "wmNewsId") Long wmNewsId){
        return ResultVo.ok(wmNewsService.getById(wmNewsId));
    }

    /**
     * 更新自媒体文章
     * @param wmNews
     * @return
     */
    @PostMapping("/api/wmNews/update")
    public ResultVo updateWmNews(@RequestBody WmNews wmNews){
        wmNewsService.updateById(wmNews);
        return ResultVo.ok("操作成功");
    }

    /**
     * 文章列表 分页查询 给后台管理人员，人工审核
     * @param dto
     * @return
     */
    @PostMapping("/news/list_vo")
    public PageResultVo findPage4Admin(@RequestBody WmNewsPageReqDto dto){
        //for 4 four 发音相同
        // to 2 u
        PageResultVo vo = wmNewsService.findPage4Admin(dto);
        return vo;
    }

    /**
     * 人工审核 - 通过
     * @param dto
     * @return
     */
    @PostMapping("/news/auth_pass")
    public ResultVo authPass(@RequestBody @Validated AuthReqDto dto){
        wmNewsService.authPass(dto);
        return ResultVo.ok();
    }

    /**
     * 人工审核 - 驳回
     * @param dto
     * @return
     */
    @PostMapping("/news/auth_fail")
    public ResultVo authFail(@RequestBody AuthReqDto dto){
        wmNewsService.authFail(dto);
        return ResultVo.ok();
    }

    /**
     * 查看文章详情
     * @param newsId
     * @return
     */
    @GetMapping("/news/one_vo/{newsId}")
    public ResultVo oneVo(@PathVariable Long newsId){
        WmNewsVo vo = wmNewsService.getOneVo(newsId);
        return ResultVo.ok(vo);
    }

    /**
     * 查询需要同步到文章
     * 自媒体文章同步到微服务
     * @param pageReqDto
     * @return
     */
    @PostMapping("/api/wmNews/list4ArticleSync")
    public ResultVo<List<WmNewsVo>> list4ArticleSync(@RequestBody WmNewsPageReqDto pageReqDto){
        List<WmNewsVo> voList=wmNewsService.list4ArticleSync(pageReqDto);
        return ResultVo.ok(voList);
    }
}
